package Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

    //银行卡
    public static File getSaveFile(Context context) {
        File file = new File(context.getFilesDir(), "yhkpic.jpg");
        return file;
    }

    //银行卡反面
    public static File getBankBackFile(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "yhkfm.jpg");
        return newFile;
    }

    //银行卡反面
    public static File getUserIconFile(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "usericon.jpg");
        return newFile;
    }


    //身份证正面
    public static File getSaveFileSfzzm(Context context) {
        File file = new File(context.getFilesDir(), "picsfzzm.jpg");
        return file;
    }

    //身份证反面
    public static File getSaveFileSfzfm(Context context) {
        File file = new File(context.getFilesDir(), "picsfzfm.jpg");
        return file;
    }

    // 人脸自拍
    public static File getSaveFileFace(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "picface.jpg");
        return newFile;
    }

    // 营业执照
    public static File getBusinessLicense(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "businesslicense.jpg");
        return newFile;
    }

    // 门头照
    public static File getDoor(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "door.jpg");
        return newFile;
    }

    // 外部照片
    public static File getExternalImageFile(String name) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, name);
        return newFile;
    }

    // 营业环境照
    public static File getEnvironment(Context context) {
        File imagePath = new File(Environment.getExternalStorageDirectory(), "images");
        if (!imagePath.exists()) {
            boolean mkdir = imagePath.mkdir();
        }
        File newFile = new File(imagePath, "business_environment.jpg");
        return newFile;
    }

    public static String imageToBase64(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try {
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    public static String compress(String path) {
        return compress(path, 500);
    }


    public static String compress(String path, int maxSize) {
        //先将所选图片转化为流的形式，path所得到的图片路径
        FileInputStream is = null;
        try {
            is = new FileInputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (is == null) {
            return path;
        }
        //定义一个file，为压缩后的图片
        File f = new File(path);
        int size = 4;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = size;
        //将图片缩小为原来的  1/size ,不然图片很大时会报内存溢出错误
        Bitmap image = BitmapFactory.decodeStream(is, null, options);

        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//这里100表示不压缩，将不压缩的数据存放到baos中
        int per = 100;
        while (baos.toByteArray().length / 1024 > maxSize) { // 循环判断如果压缩后图片是否大于500kb,大于继续压缩
            baos.reset();// 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, per, baos);// 将图片压缩为原来的(100-per)%，把压缩后的数据存放到baos中
            per -= 10;// 每次都减少10

        }
        //回收图片，清理内存
        if (!image.isRecycled()) {
            image.recycle();
        }
        // 把压缩后的数据baos存放到ByteArrayInputStream中
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
//        btout.close();
//        FileOutputStream os;
//        os = new FileOutputStream(f);
//        //自定义工具类，将输入流复制到输出流中
//        StreamTransferUtils.CopyStream(btinput, os);
//        btinput.close();
//        os.close();

        byte[] data;
        String result = null;
        try {
            //创建一个字符流大小的数组。
            data = new byte[isBm.available()];
            //写入数组
            int read = isBm.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                isBm.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.replaceAll("\n", "");
    }

    public static File updateDir = null;
    public static File updateFile = null;
    public static final String EnboApplication = "enboUpdateApplication";

    public static boolean isCreateFileSucess;

    public FileUtil() {
    }

    public static boolean createFile() {
        try {
            File file = new File(FilePath.ROOTPATH);
            file.mkdir();
            file = new File(FilePath.SYSTEMPATH);
            file.mkdir();
            file = new File(FilePath.PICTUREPATH);
            file.mkdir();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void createFile_a(String app_name) {

        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            isCreateFileSucess = true;

            updateDir = new File(Environment.getExternalStorageDirectory()
                    + "/" + EnboApplication + "/");
            updateFile = new File(updateDir + "/" + app_name + ".apk");

            if (!updateDir.exists()) {
                updateDir.mkdirs();
            }
            if (!updateFile.exists()) {
                try {
                    updateFile.createNewFile();
                } catch (IOException e) {
                    isCreateFileSucess = false;
                    e.printStackTrace();
                }
            }

        } else {
            isCreateFileSucess = false;
        }
    }

}
